﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;


namespace Shopping.Common
{
    /// <summary>
    /// 序列化辅助类 by 赵国伟 2010-10-14
    /// </summary>
    public class SerializeHelper
    {
        #region "将指定的对象序列化为xml文件 by 赵国伟 2010-10-14"
        /// <summary>
        /// 将指定的对象序列化为xml文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string SerializeToXML<T>(T obj)
        {
            XmlSerializer serializer = new XmlSerializer(obj.GetType());
            StringWriter writer = new StringWriter();
            serializer.Serialize(writer, obj);
            return writer.ToString();
        }
        #endregion

        #region "将指定的xml文件反序列化为指定的对象 by 赵国伟 2010-10-14"
        /// <summary>
        /// 将指定的xml文件反序列化为指定的对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static T DeSerializeForXML<T>(string str)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            TextReader tx = new StringReader(str);
            return (T)serializer.Deserialize(tx);
        }
        #endregion

    }
}
